<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Groups extends CI_Model
{
	function __construct()
	{
		parent::__construct();
	}
	
	function get_menu()
	{
		$query= $this->db->get_where('suba_menu', array('menuAction'=>'#'));
		
		$menu = array();
		
		if ($query->num_rows() != 0)
		{
			foreach($query->result() as $valor)
			{
				$this->db->select('*');
				$this->db->from('suba_menu');
				$this->db->like('menuTag', $valor->menuTag, 'both');
				$query_menu_detail = $this->db->get();
				
	
				if($query_menu_detail->num_rows() > 0)
				{
					$aux = array();
					
					foreach($query_menu_detail->result() as $det)
					{
						if($det->menuAction != "#")
						{
							$aux[] = array(
									'menuId'      => $det->menuId,
									'description' => $det->menuDescription,
									'menuAction'  => $det->menuAction,
									'menuTag'     => $det->menuTag
								       );
						}
					}
					
					$menu[] = array(
						 'menuId'	=>   $valor->menuId,
						 'description'	=>   $valor->menuDescription,
						 'details'	=>   $aux
						);	
				}
			}
			
			return $menu;
		}
		else
		{
			return 0;	
		}
	}	
	
	function get_list()
	{
		$query= $this->db->get_where('suba_groups');
		
		if ($query->num_rows()!=0)
		{
			return $query->result_array();	
		}
		else
		{
			return false;
		}
	}

	function set_group($data=null)
	{
		if($data == null)
		{
			return false;
		}
		else
		{
			$regsitrar = true;
			$msj = "";
			$indice = 0;
			$array = array();
			$nombre_grupo = "";
			
			foreach($data['xinput'] as $in)
			{
				switch($in)
				{
					case "descripcion":
						$query = $this->db->get_where('suba_groups', array('gpoDescription'=>$data['xvalue'][$indice]));
						if ($query->num_rows() != 0)
						{
							$msj = "name";
							$regsitrar = false;
						}
						else
						{
							$array['gpoDescription'] = $data['xvalue'][$indice];
							$nombre_grupo = $data['xvalue'][$indice];
						}
						$indice++;
						break;
					
					default:
						$indice++;
						break;
				}
			}
			
			if($regsitrar == true)
			{
				if($this->db->insert('suba_groups',$array) == false)
					return 1;
				else
				{
					//registrar permisos
					$query = $this->db->get_where('suba_groups', array('gpoDescription'=>$nombre_grupo));
					if ($query->num_rows() != 0)
						{
							$id = $query->row('gpoId');
							$cabeceras = array();

							foreach($data['xpermisos'] as $idM)
							{
								//obtener cabecera
								$query_cab = $this->db->get_where('suba_menu', array('menuId'=>$idM));
								if($query_cab->num_rows() != 0)
								{
									$menuTag = $query_cab->row('menuTag');
									$menuTag = explode('_',$menuTag);
									
									
									$query_cab_index = $this->db->get_where('suba_menu', array('menuTag'=>$menuTag[0]));
									if($query_cab_index->num_rows() != 0)
									{
										$idCabeza = $query_cab_index->row('menuId');
										
										if(in_array($idCabeza, $cabeceras) == false)
										{
											//no esta en las cabeceras
											$cabeceras[] = $idCabeza;
											$this->db->insert('suba_groups_menu',array('gpoId'=>$id,'menuId'=>$idCabeza));
											$this->db->insert('suba_groups_menu',array('gpoId'=>$id,'menuId'=>$idM));
										}
										else
										{
											//ya se inserto la cabecera
											$this->db->insert('suba_groups_menu',array('gpoId'=>$id,'menuId'=>$idM));
										}
									}
									
								}
							}
							return 0;
						}
						else
						{
							return "permisos";
						}
				}
			}
			else
			{
				return $msj;
			}
			
		}
		
	}
	
	function get_data($id=0)
	{
		$query= $this->db->get_where('suba_groups',array('gpoId'=>$id['xinput'] ));
		if ($query->num_rows()!=0)
		{
			//Buscar los permisos para el grupo seleccionado
			$permisos  = $this->db->get_where('suba_groups_menu', array('gpoId'=>$query->row('gpoId')));
			if($permisos->num_rows() != 0)
			{
				$services = array();
				
				foreach($permisos->result_array() as $p)
				{
					array_push($services, $p['menuId']);
				}
				
				$groups = array(
					'gpoId'    	 => $query->row('gpoId'),
					'gpoDescription' => $query->row('gpoDescription'),
					'permission'	 => $services
					);
				
				return $groups;
			}
			else
			{
				return $query->row();
			}
		}
		else
		{
			return false;
		}
	}
	
	function update_group($data=null)
	{
		if($data == null)
		{
			return false;
		}
		else
		{
			$msj = "";
			$indice = 0;
			$id = 0;
			$array = array();
			
			foreach($data['xinput'] as $in)
			{
				switch($in)
				{
					case "descripcion":
						$array['gpoDescription'] = $data['xvalue'][$indice];
						$indice++;
						break;
					
					case "id":
						$id = $data['xvalue'][$indice];
						$indice++;
						break;
					
					default:
						$indice++;
						break;
				}
			}
			
			$this->db->set($array);
			$this->db->where('gpoId', $id);
			if($this->db->update('suba_groups', $array) == false)
				{
					//esto es un error
					return 1;
				}
			else
			{
				$this->db->where('gpoId', $id);
				$this->db->delete('suba_groups_menu');
				
					//Agregar las nuevas entradas
					
					$cabeceras = array();
					foreach($data['xpermisos'] as $idM)
					{
						//obtener cabecera
						$query_cab = $this->db->get_where('suba_menu', array('menuId'=>$idM));
						if($query_cab->num_rows() != 0)
						{
							$menuTag = $query_cab->row('menuTag');
							$menuTag = explode('_',$menuTag);
							
							
							$query_cab_index = $this->db->get_where('suba_menu', array('menuTag'=>$menuTag[0]));
							if($query_cab_index->num_rows() != 0)
							{
								$idCabeza = $query_cab_index->row('menuId');
								
								if(in_array($idCabeza, $cabeceras) == false)
								{
									//no esta en las cabeceras
									$cabeceras[] = $idCabeza;
									$this->db->insert('suba_groups_menu',array('gpoId'=>$id,'menuId'=>$idCabeza));
									$this->db->insert('suba_groups_menu',array('gpoId'=>$id,'menuId'=>$idM));
								}
								else
								{
									//ya se inserto la cabecera
									$this->db->insert('suba_groups_menu',array('gpoId'=>$id,'menuId'=>$idM));
								}
							}
							
						}
					}
					
				return 0;
			}
			
		}
		
	}
	
	function delete_group($data=null)
	{
		if($data == null)
		{
			return false;
		}
		else
		{
			$msj = "";
			$indice = 0;
			$id = 0;
			$array = array();
			
			foreach($data['xinput'] as $in)
			{
				switch($in)
				{					
					case "id":
						$id = $data['xvalue'][$indice];
						$indice++;
						break;
					
					default:
						$indice++;
						break;
				}
			}
			
			
			//Borrar los permisos
			$this->db->trans_start();
			
			$this->db->where('gpoId', $id);
			$this->db->delete('suba_groups_menu');
			
			//borrar el grupo
			$this->db->where('gpoId', $id);
			$this->db->delete('suba_groups');
			
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
			    return 1;
			}
			else
			{
				return 0;
			}
		}
		
	}
}